package com.hwg.emp.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hwg.emp.common.util.DBUtil;
import com.hwg.emp.model.DeptMD;
import com.hwg.emp.model.EmployeeMD;
import com.hwg.emp.model.UserMD;
import com.hwg.emp.service.DeptService;
import com.hwg.emp.service.EmployeeService;
import com.hwg.emp.service.ServiceFactroy;
import com.hwg.emp.service.UserService;

/**
 * 员工管理 (增加员工)
 * @author Administrator
 *
 */
public class selectEmpsController extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	//设置字符
    	req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
        
		//实例员工和用户
    	EmployeeMD  employee = new EmployeeMD();
    	UserMD user = new UserMD();
    	
    	//获取服务
    	UserService userservice = ServiceFactroy.getService(UserService.class);
    	EmployeeService employeeService = ServiceFactroy.getService(EmployeeService.class);
    	DeptService deptService = ServiceFactroy.getService(DeptService.class);
    	
    	String dept_name = req.getParameter("dept");
    	try {
    		//根据输入的部门名获取部门信息
			DeptMD dept=deptService.selectOneDept(dept_name);
			//获取员工的其他信息
	    	String e_id = DBUtil.uuid();
	    	String sex = req.getParameter("sex");
	    	String age = req.getParameter("age");
			String s_birth = req.getParameter("birth");
			java.sql.Date birth=java.sql.Date.valueOf(s_birth); //将日期转为sql.date格式
	    	String phone = req.getParameter("phone");
	    	
	    	//设置员工属性
	    	employee.setId(e_id);
	    	if (sex.equals("男")) { 
	    		employee.setSex("1");
			}else if (sex.equals("女")) {
				employee.setSex("0");
			}
	    	employee.setAge(age);
	    	employee.setBirth(birth);
	    	employee.setDept(dept);
	    	employee.setPhone(phone);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	
    	//获取输入的用户的信息
    	String u_id = employee.getId();
    	String account = req.getParameter("account");
    	String password = req.getParameter("newPsw2");
    	String name = req.getParameter("name");
    	
    	//设置用户属性
    	user.setId(u_id);
    	user.setAccount(account);
    	user.setPassword(password);
    	user.setName(name);
    	
    	//调用服务
    	try {
			employeeService.insertEmployee(employee);
			userservice.insertUser(user);
			req.setAttribute("Msg", "增加员工成功");
	    	req.getRequestDispatcher("/welcome.jsp").forward(req, resp);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	//查询部门下所有员工
    	
     }  	
 }
